package com.google.appengine.echogear;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.echogear.core.Cache;
import com.google.appengine.echogear.core.EchoJob;
import com.google.appengine.echogear.core.Register;

public class ResetServlet extends HttpServlet {

	private static final long serialVersionUID = -3499359619720586505L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
			Iterable<Entity> result = Register.load();
			Cache.reset();
			for(Entity record : result) {
				try {
					Object name = record.getProperty("name");
					Object className = record.getProperty("class");
					Object param = record.getProperty("param");		
					Object user = record.getProperty("user");
					EchoJob echoJob = (EchoJob)Class.forName((String) className).newInstance();
					echoJob.setName((String) name);
					echoJob.setParam((String) param);
					echoJob.setUser((String) user);
					if(echoJob.init()) {
						Cache.add((String)name, echoJob);
					} else {
						continue;
					}
				} catch (ClassNotFoundException e) {
					continue;
				} catch (InstantiationException e) {
					continue;
				} catch (IllegalAccessException e) {
					continue;
				}
			}
	}

}